package work.jame.blog.framework.result;

import java.io.Serializable;
import java.util.List;

/**
 * 带有分页的结果
 *
 * @author : Jame
 * @date : 2022-08-13 16:20
 **/
public class PageResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private List<T> data;
    private int code;
    private String message;
    /**
     * 当前页数
     */
    private long currentPageNumber;
    /**
     * 页大小
     */
    private long pageSize;
    /**
     * 总条数
     */
    private long lineCount;


    public PageResult(List<T> data, Integer code, String message,
                      Long currentPageNumber, Long pageSize, Long lineCount) {
        this.data = data;
        this.code = code;
        this.message = message;
        this.currentPageNumber = currentPageNumber;
        this.pageSize = pageSize;
        this.lineCount = lineCount;

    }

    public PageResult(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    private static <T> PageResult<T> create(List<T> data, String message,
                                            Long currentPageNumber, Long pageSize, Long lineCount) {
        return new PageResult<T>(data, ServerStatusCode.SUCCESS, message, currentPageNumber, pageSize, lineCount);
    }

    private static <T> PageResult<T> create(Integer code, String message) {
        return new PageResult<T>(code, message);
    }

    /**
     * 分页result
     *
     * @param data              数据
     * @param currentPageNumber 当前页数
     * @param pageSize          页大小
     * @param lineCount         总行数
     * @param <T>               T
     * @return PageResult
     */
    public static <T> PageResult<T> succeed(List<T> data, Long currentPageNumber,
                                            Long pageSize, Long lineCount) {
        return create(data, "操作成功", currentPageNumber, pageSize, lineCount);
    }


    public static <T> PageResult<T> succeed(List<T> data, String message, Long currentPageNumber,
                                            Long pageSize, Long lineCount) {
        return create(data, message, currentPageNumber, pageSize, lineCount);
    }



    public static <T> PageResult<T> fail(String message) {
        return create(ServerStatusCode.FAIL, message);
    }

    public static <T> PageResult<T> fail() {
        return create(ServerStatusCode.FAIL, "操作失败");
    }

    public static <T> PageResult<T> noData() {
        return create(ServerStatusCode.FAIL, "没有数据");
    }

    public static <T> PageResult<T> error() {
        return create(ServerStatusCode.ERROR, "服务出错");
    }


    public void setData(List<T> data) {
        this.data = data;
    }

    public List<T> getData() {
        return data;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Long getCurrentPageNumber() {
        return currentPageNumber;
    }

    public void setCurrentPageNumber(Long currentPageNumber) {
        this.currentPageNumber = currentPageNumber;
    }

    public Long getPageSize() {
        return pageSize;
    }

    public void setPageSize(Long pageSize) {
        this.pageSize = pageSize;
    }

    public Long getLineCount() {
        return lineCount;
    }

    public void setLineCount(Long lineCount) {
        this.lineCount = lineCount;
    }


    @Override
    public String toString() {
        return "PageResult{" +
                "data=" + data +
                ", code=" + code +
                ", message='" + message + '\'' +
                ", currentPageNumber=" + currentPageNumber +
                ", pageSize=" + pageSize +
                ", lineCount=" + lineCount +
                '}';
    }
}

